package com.iknow.xmpp.service;

import android.os.AsyncTask.Status;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import com.iknow.IKnow;
import com.iknow.db.IKnowDataBase;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.util.DomXmlUtil;
import com.iknow.util.Loger;
import com.iknow.xmpp.service.aidl.IChat.Stub;
import com.iknow.xmpp.service.aidl.IMessageListener;
import java.io.File;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Type;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.ChatState;
import org.jivesoftware.smackx.ChatStateListener;
import org.w3c.dom.Node;

public class ChatAdapter extends IChat.Stub
{
  private static final int HISTORY_MAX_SIZE = 50;
  private static final String TAG = "ChatAdapter";
  private String mAccountUser;
  private final Chat mAdaptee;
  private SendAudioTask mAudioTask;
  private MessageStatusCallback mCallback;
  private File mHistoryPath;
  private SendImageTask mImageTask;
  private boolean mIsHistory;
  private boolean mIsOpen;
  private final List<Message> mMessages;
  private final MsgListener mMsgListener;
  private final Contact mParticipant;
  private List<Message> mPenddingSendMsgs;
  private List<AudioMessage> mPendingSendAudioMsgs;
  private List<ImageMessage> mPendingSendImageMsgs;
  private final RemoteCallbackList<IMessageListener> mRemoteListeners = new RemoteCallbackList();
  private String mState;
  private SendMessageTask mTask;
  private TaskListener mTaskListener;

  public ChatAdapter(Chat paramChat)
  {
    this.mMsgListener = new MsgListener(this);
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "SendMessageTask";
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        super.onPostExecute(paramGenericTask, paramTaskResult);
        if (ChatAdapter.access$1(this.this$0).size() > 0)
          ChatAdapter.access$2(this.this$0);
        if (ChatAdapter.access$3(this.this$0).size() > 0)
          ChatAdapter.access$4(this.this$0);
        if (ChatAdapter.access$5(this.this$0).size() > 0)
          ChatAdapter.access$6(this.this$0);
      }

      public void onProgressUpdate(, Object paramObject)
      {
        super.onProgressUpdate(paramGenericTask, paramObject);
        Message localMessage = (Message)((TaskParams)paramObject).get("message");
        if (ChatAdapter.access$0(this.this$0) != null)
          ChatAdapter.access$0(this.this$0).refreshStatus(localMessage);
      }
    };
    this.mAdaptee = paramChat;
    this.mParticipant = new Contact(paramChat.getParticipant());
    this.mMessages = new LinkedList();
    this.mPenddingSendMsgs = new LinkedList();
    this.mPendingSendAudioMsgs = new LinkedList();
    this.mPendingSendImageMsgs = new LinkedList();
    this.mAdaptee.addMessageListener(this.mMsgListener);
  }

  private void doSendAudioMessage()
  {
    if ((this.mAudioTask != null) && (this.mAudioTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mAudioTask = new SendAudioTask(this, null);
      this.mAudioTask.setListener(this.mTaskListener);
      this.mAudioTask.execute(new TaskParams[0]);
    }
  }

  private void doSendImageMessage()
  {
    if ((this.mImageTask != null) && (this.mImageTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mImageTask = new SendImageTask(this, null);
      this.mImageTask.setListener(this.mTaskListener);
      this.mImageTask.execute(new TaskParams[0]);
    }
  }

  private void doSendMessage()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mTask = new SendMessageTask(this, null);
      this.mTask.setListener(this.mTaskListener);
      this.mTask.execute(new TaskParams[0]);
    }
  }

  private void doSendMessage(Message paramMessage)
    throws XMPPException
  {
    org.jivesoftware.smack.packet.Message localMessage = new org.jivesoftware.smack.packet.Message();
    localMessage.setTo(paramMessage.getTo());
    localMessage.setBody(paramMessage.getBody());
    localMessage.setThread(paramMessage.getThread());
    localMessage.setSubject(paramMessage.getSubject());
    localMessage.setType(Message.Type.chat);
    this.mAdaptee.sendMessage(localMessage);
    paramMessage.setThread(localMessage.getThread());
    saveHistory(paramMessage, this.mAccountUser, "1", false);
  }

  private void doSendMultimediaMessage(Message paramMessage)
  {
    if (!(paramMessage instanceof AudioMessage))
      paramMessage instanceof ImageMessage;
    try
    {
      doSendMessage(paramMessage);
      return;
    }
    catch (XMPPException localXMPPException)
    {
      Loger.e("ChatAdapter", localXMPPException.getMessage());
      localXMPPException.printStackTrace();
    }
  }

  void addMessage(Message paramMessage)
  {
    if (this.mMessages.size() == 50)
      this.mMessages.remove(0);
    this.mMessages.add(paramMessage);
    if ((!("".equals(paramMessage.getBody()))) && (paramMessage.getBody() != null))
      saveHistory(paramMessage, paramMessage.getFrom(), "0", true);
  }

  public void addMessageListener(IMessageListener paramIMessageListener)
    throws RemoteException
  {
    if (paramIMessageListener != null)
      this.mRemoteListeners.register(paramIMessageListener);
  }

  public String getAccountUser()
  {
    return this.mAccountUser;
  }

  public Chat getAdaptee()
  {
    return this.mAdaptee;
  }

  public List<Message> getMessages()
    throws RemoteException
  {
    return Collections.unmodifiableList(this.mMessages);
  }

  public Contact getParticipant()
    throws RemoteException
  {
    return this.mParticipant;
  }

  public String getState()
    throws RemoteException
  {
    return this.mState;
  }

  public boolean isOpen()
    throws RemoteException
  {
    return this.mIsOpen;
  }

  public void removeMessageListener(IMessageListener paramIMessageListener)
    throws RemoteException
  {
    if (paramIMessageListener != null)
      this.mRemoteListeners.unregister(paramIMessageListener);
  }

  public void saveHistory(Message paramMessage, String paramString1, String paramString2, boolean paramBoolean)
  {
    IKnow.mIKnowDataBase.addMessage(paramMessage, paramString2);
    if (paramBoolean)
      IKnow.mIKnowDataBase.addSession(paramMessage.getiKnowType(), paramMessage.getBody(), StringUtils.parseBareAddress(paramMessage.getTo()), StringUtils.parseBareAddress(paramMessage.getFrom()), false, paramMessage.getTimestamp());
    while (true)
    {
      return;
      IKnow.mIKnowDataBase.addSession(paramMessage.getiKnowType(), paramMessage.getBody(), StringUtils.parseBareAddress(paramMessage.getFrom()), StringUtils.parseBareAddress(paramMessage.getTo()), true, paramMessage.getTimestamp());
    }
  }

  // ERROR //
  public void sendMessage(Message paramMessage)
    throws RemoteException
  {
    // Byte code:
    //   0: aload_0
    //   1: getfield 101	com/iknow/xmpp/service/ChatAdapter:mPendingSendAudioMsgs	Ljava/util/List;
    //   4: astore_2
    //   5: aload_2
    //   6: monitorenter
    //   7: aload_1
    //   8: instanceof 240
    //   11: ifeq +26 -> 37
    //   14: aload_0
    //   15: getfield 101	com/iknow/xmpp/service/ChatAdapter:mPendingSendAudioMsgs	Ljava/util/List;
    //   18: aload_1
    //   19: checkcast 240	com/iknow/xmpp/service/AudioMessage
    //   22: invokeinterface 269 2 0
    //   27: pop
    //   28: aload_0
    //   29: invokespecial 126	com/iknow/xmpp/service/ChatAdapter:doSendAudioMessage	()V
    //   32: aload_2
    //   33: monitorexit
    //   34: goto +109 -> 143
    //   37: aload_2
    //   38: monitorexit
    //   39: aload_0
    //   40: getfield 103	com/iknow/xmpp/service/ChatAdapter:mPendingSendImageMsgs	Ljava/util/List;
    //   43: astore 4
    //   45: aload 4
    //   47: monitorenter
    //   48: aload_1
    //   49: instanceof 242
    //   52: ifeq +40 -> 92
    //   55: aload_0
    //   56: getfield 103	com/iknow/xmpp/service/ChatAdapter:mPendingSendImageMsgs	Ljava/util/List;
    //   59: aload_1
    //   60: checkcast 242	com/iknow/xmpp/service/ImageMessage
    //   63: invokeinterface 269 2 0
    //   68: pop
    //   69: aload_0
    //   70: invokespecial 131	com/iknow/xmpp/service/ChatAdapter:doSendImageMessage	()V
    //   73: aload 4
    //   75: monitorexit
    //   76: goto +67 -> 143
    //   79: astore 5
    //   81: aload 4
    //   83: monitorexit
    //   84: aload 5
    //   86: athrow
    //   87: astore_3
    //   88: aload_2
    //   89: monitorexit
    //   90: aload_3
    //   91: athrow
    //   92: aload 4
    //   94: monitorexit
    //   95: aload_0
    //   96: getfield 99	com/iknow/xmpp/service/ChatAdapter:mPenddingSendMsgs	Ljava/util/List;
    //   99: astore 6
    //   101: aload 6
    //   103: monitorenter
    //   104: aload_1
    //   105: instanceof 190
    //   108: ifeq +32 -> 140
    //   111: aload_0
    //   112: getfield 99	com/iknow/xmpp/service/ChatAdapter:mPenddingSendMsgs	Ljava/util/List;
    //   115: aload_1
    //   116: invokeinterface 269 2 0
    //   121: pop
    //   122: aload_0
    //   123: invokespecial 121	com/iknow/xmpp/service/ChatAdapter:doSendMessage	()V
    //   126: aload 6
    //   128: monitorexit
    //   129: goto +14 -> 143
    //   132: astore 7
    //   134: aload 6
    //   136: monitorexit
    //   137: aload 7
    //   139: athrow
    //   140: aload 6
    //   142: monitorexit
    //   143: return
    //
    // Exception table:
    //   from	to	target	type
    //   48	84	79	finally
    //   92	95	79	finally
    //   7	39	87	finally
    //   88	90	87	finally
    //   104	137	132	finally
    //   140	143	132	finally
  }

  public void setAccountUser(String paramString)
  {
    this.mAccountUser = paramString;
  }

  public void setOpen(boolean paramBoolean)
    throws RemoteException
  {
    this.mIsOpen = paramBoolean;
  }

  public void setState(String paramString)
    throws RemoteException
  {
    this.mState = paramString;
  }

  private class MsgListener
  implements ChatStateListener
  {
    public void processMessage(, org.jivesoftware.smack.packet.Message paramMessage)
    {
      int j;
      Object localObject = null;
      if (paramMessage.getType() != Message.Type.error)
      {
        if (paramMessage.getBody().indexOf("<image") == -1)
          break label105;
        localObject = new ImageMessage(paramMessage);
        Node localNode1 = DomXmlUtil.paseXml(paramMessage.getBody());
        ((Message)localObject).setiKnowMsgType(17);
        ((ImageMessage)localObject).setFileName(DomXmlUtil.getAttributes(localNode1, "src"));
      }
      while (true)
      {
        while (true)
        {
          this.this$0.addMessage((Message)localObject);
          int i = ChatAdapter.access$9(this.this$0).beginBroadcast();
          j = 0;
          if (j < i)
            break label184;
          ChatAdapter.access$9(this.this$0).finishBroadcast();
          return;
          label105: if (paramMessage.getBody().indexOf("<audio") == -1)
            break;
          localObject = new AudioMessage(paramMessage);
          Node localNode2 = DomXmlUtil.paseXml(paramMessage.getBody());
          ((Message)localObject).setiKnowMsgType(18);
          ((AudioMessage)localObject).setFileName(DomXmlUtil.getAttributes(localNode2, "src"));
        }
        localObject = new Message(paramMessage);
        ((Message)localObject).setiKnowMsgType(16);
        ((Message)localObject).setiKnowMsgType(16);
      }
      label184: IMessageListener localIMessageListener = (IMessageListener)ChatAdapter.access$9(this.this$0).getBroadcastItem(j);
      if (localIMessageListener != null);
      try
      {
        localIMessageListener.processMessage(this.this$0, (Message)localObject);
        ++j;
      }
      catch (RemoteException localRemoteException)
      {
        while (true)
          Loger.w("ChatAdapter", "Error while diffusing message to listener", localRemoteException);
      }
    }

    public void stateChanged(, ChatState paramChatState)
    {
      ChatAdapter.access$10(this.this$0, paramChatState.name());
      int i = ChatAdapter.access$9(this.this$0).beginBroadcast();
      int j = 0;
      if (j >= i)
      {
        ChatAdapter.access$9(this.this$0).finishBroadcast();
        return;
      }
      IMessageListener localIMessageListener = (IMessageListener)ChatAdapter.access$9(this.this$0).getBroadcastItem(j);
      try
      {
        localIMessageListener.stateChanged(this.this$0);
        ++j;
      }
      catch (RemoteException localRemoteException)
      {
        while (true)
          Loger.w("ChatAdapter", localRemoteException.getMessage());
      }
    }
  }

  private class SendAudioTask extends GenericTask
  {
    // ERROR //
    protected TaskResult _doInBackground()
    {
      // Byte code:
      //   0: aload_0
      //   1: getfield 13	com/iknow/xmpp/service/ChatAdapter$SendAudioTask:this$0	Lcom/iknow/xmpp/service/ChatAdapter;
      //   4: invokestatic 25	com/iknow/xmpp/service/ChatAdapter:access$3	(Lcom/iknow/xmpp/service/ChatAdapter;)Ljava/util/List;
      //   7: invokeinterface 31 1 0
      //   12: ifgt +7 -> 19
      //   15: getstatic 37	com/iknow/task/TaskResult:OK	Lcom/iknow/task/TaskResult;
      //   18: areturn
      //   19: aload_0
      //   20: getfield 13	com/iknow/xmpp/service/ChatAdapter$SendAudioTask:this$0	Lcom/iknow/xmpp/service/ChatAdapter;
      //   23: invokestatic 25	com/iknow/xmpp/service/ChatAdapter:access$3	(Lcom/iknow/xmpp/service/ChatAdapter;)Ljava/util/List;
      //   26: astore_2
      //   27: aload_2
      //   28: monitorenter
      //   29: aload_0
      //   30: getfield 13	com/iknow/xmpp/service/ChatAdapter$SendAudioTask:this$0	Lcom/iknow/xmpp/service/ChatAdapter;
      //   33: invokestatic 25	com/iknow/xmpp/service/ChatAdapter:access$3	(Lcom/iknow/xmpp/service/ChatAdapter;)Ljava/util/List;
      //   36: iconst_0
      //   37: invokeinterface 41 2 0
      //   42: checkcast 43	com/iknow/xmpp/service/AudioMessage
      //   45: astore 4
      //   47: aload_0
      //   48: getfield 13	com/iknow/xmpp/service/ChatAdapter$SendAudioTask:this$0	Lcom/iknow/xmpp/service/ChatAdapter;
      //   51: invokestatic 25	com/iknow/xmpp/service/ChatAdapter:access$3	(Lcom/iknow/xmpp/service/ChatAdapter;)Ljava/util/List;
      //   54: iconst_0
      //   55: invokeinterface 46 2 0
      //   60: pop
      //   61: aload_2
      //   62: monitorexit
      //   63: aload_0
      //   64: getfield 13	com/iknow/xmpp/service/ChatAdapter$SendAudioTask:this$0	Lcom/iknow/xmpp/service/ChatAdapter;
      //   67: aload 4
      //   69: invokestatic 50	com/iknow/xmpp/service/ChatAdapter:access$8	(Lcom/iknow/xmpp/service/ChatAdapter;Lcom/iknow/xmpp/service/Message;)V
      //   72: goto -72 -> 0
      //   75: astore_3
      //   76: aload_2
      //   77: monitorexit
      //   78: aload_3
      //   79: athrow
      //
      // Exception table:
      //   from	to	target	type
      //   29	63	75	finally
      //   76	78	75	finally
    }
  }

  private class SendImageTask extends GenericTask
  {
    // ERROR //
    protected TaskResult _doInBackground()
    {
      // Byte code:
      //   0: aload_0
      //   1: getfield 13	com/iknow/xmpp/service/ChatAdapter$SendImageTask:this$0	Lcom/iknow/xmpp/service/ChatAdapter;
      //   4: invokestatic 25	com/iknow/xmpp/service/ChatAdapter:access$5	(Lcom/iknow/xmpp/service/ChatAdapter;)Ljava/util/List;
      //   7: invokeinterface 31 1 0
      //   12: ifgt +7 -> 19
      //   15: getstatic 37	com/iknow/task/TaskResult:OK	Lcom/iknow/task/TaskResult;
      //   18: areturn
      //   19: aload_0
      //   20: getfield 13	com/iknow/xmpp/service/ChatAdapter$SendImageTask:this$0	Lcom/iknow/xmpp/service/ChatAdapter;
      //   23: invokestatic 25	com/iknow/xmpp/service/ChatAdapter:access$5	(Lcom/iknow/xmpp/service/ChatAdapter;)Ljava/util/List;
      //   26: astore_2
      //   27: aload_2
      //   28: monitorenter
      //   29: aload_0
      //   30: getfield 13	com/iknow/xmpp/service/ChatAdapter$SendImageTask:this$0	Lcom/iknow/xmpp/service/ChatAdapter;
      //   33: invokestatic 25	com/iknow/xmpp/service/ChatAdapter:access$5	(Lcom/iknow/xmpp/service/ChatAdapter;)Ljava/util/List;
      //   36: iconst_0
      //   37: invokeinterface 41 2 0
      //   42: checkcast 43	com/iknow/xmpp/service/ImageMessage
      //   45: astore 4
      //   47: aload_0
      //   48: getfield 13	com/iknow/xmpp/service/ChatAdapter$SendImageTask:this$0	Lcom/iknow/xmpp/service/ChatAdapter;
      //   51: invokestatic 25	com/iknow/xmpp/service/ChatAdapter:access$5	(Lcom/iknow/xmpp/service/ChatAdapter;)Ljava/util/List;
      //   54: iconst_0
      //   55: invokeinterface 46 2 0
      //   60: pop
      //   61: aload_2
      //   62: monitorexit
      //   63: aload 4
      //   65: ifnull -65 -> 0
      //   68: aload_0
      //   69: getfield 13	com/iknow/xmpp/service/ChatAdapter$SendImageTask:this$0	Lcom/iknow/xmpp/service/ChatAdapter;
      //   72: aload 4
      //   74: invokestatic 50	com/iknow/xmpp/service/ChatAdapter:access$8	(Lcom/iknow/xmpp/service/ChatAdapter;Lcom/iknow/xmpp/service/Message;)V
      //   77: goto -77 -> 0
      //   80: astore_3
      //   81: aload_2
      //   82: monitorexit
      //   83: aload_3
      //   84: athrow
      //
      // Exception table:
      //   from	to	target	type
      //   29	63	80	finally
      //   81	83	80	finally
    }
  }

  private class SendMessageTask extends GenericTask
  {
    protected TaskResult _doInBackground()
    {
      Message localMessage;
      if (ChatAdapter.access$1(this.this$0).size() <= 0)
        return TaskResult.OK;
      List localList = ChatAdapter.access$1(this.this$0);
      monitorenter;
      try
      {
        do
        {
          localMessage = (Message)ChatAdapter.access$1(this.this$0).get(0);
          ChatAdapter.access$1(this.this$0).remove(0);
        }
        while (localMessage == null);
      }
      finally
      {
        try
        {
          while (true)
          {
            ChatAdapter.access$7(this.this$0, localMessage);
            localMessage.setStatus(1);
            TaskParams localTaskParams = new TaskParams();
            localTaskParams.put("msg", localMessage);
            Object[] arrayOfObject = new Object[1];
            arrayOfObject[0] = localTaskParams;
            publishProgress(arrayOfObject);
          }
          localObject = finally;
          monitorexit;
          throw localObject;
        }
        catch (XMPPException localXMPPException)
        {
          Loger.e("ChatAdapter", localXMPPException.getMessage());
          localXMPPException.printStackTrace();
        }
      }
    }
  }
}